package com.ymatou.iapi.productspu.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productspu.parameter.PassBean;
import com.ymatou.iapi.productspu.parameter.SyncPspSpuProductBean;
import com.ymatou.iapi.productspu.service.PassCall;
import com.ymatou.iapi.productspu.service.SyncPspSpuProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
/**
*SPU审核通过
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Pass{
	private static PassBean passBean;
	private static PassCall passCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("SPU审核通过");
	}

	@Before
	public void caseUp() {
		passBean=new PassBean();
		passCall=new PassCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(passCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Pass_001() {
		Logger.start(true,"SPU审核通过");
		try {
			Logger.comment("创建新商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("同步数据新商品:"+ProductId);
			SyncPspSpuProductBean syncpspspuproductBean=new SyncPspSpuProductBean();
			SyncPspSpuProductCall syncpspspuproductCall=new SyncPspSpuProductCall();
			syncpspspuproductBean.setProductId(ProductId);
			syncpspspuproductCall.setData(syncpspspuproductBean);
			syncpspspuproductCall.callService();
			Logger.verifyIsContains("ok", syncpspspuproductCall.getReturnData(), "验证返回");
			Logger.comment("数据库查询spuid");
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
			Logger.debug(spuinfo);
			Integer spuId=(Integer) spuinfo.get("SpuId");
			Integer status=(Integer) spuinfo.get("Status");
			Logger.debug(spuinfo);
			Logger.verifyEquals(-2, status, "验证新同步数据status为-2");
			
			passBean.setAuditor("autoTest");
			passBean.setSPUId(spuId);
			passCall.setData(passBean);
			passCall.callService();			
			Logger.verifyEquals("200", passCall.getString("Status"), "验证Status");
			
			spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
			status=(Integer) spuinfo.get("Status");
			Logger.verifyEquals(0, status, "验证status变成0");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Pass_002() {
		Logger.start(true,"不输入spuid失败");
		try {
			passBean.setAuditor("autoTest");
			passCall.setData(passBean);
			passCall.callService();			
			Logger.verifyEquals("400", passCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Pass_003() {
		Logger.start(true,"不输入Auditor失败");
		try {
			passBean.setSPUId(262936);
			passCall.setData(passBean);
			passCall.callService();			
			Logger.verifyEquals("400", passCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Pass_004() {
		Logger.start(true,"状态为-1已删除的SPU不能被审核通过");
		try {
			Logger.comment("创建新商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(3383);
			String ProductId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("同步数据新商品:"+ProductId);
			SyncPspSpuProductBean syncpspspuproductBean=new SyncPspSpuProductBean();
			SyncPspSpuProductCall syncpspspuproductCall=new SyncPspSpuProductCall();
			syncpspspuproductBean.setProductId(ProductId);
			syncpspspuproductCall.setData(syncpspspuproductBean);
			syncpspspuproductCall.callService();
			Logger.verifyIsContains("ok", syncpspspuproductCall.getReturnData(), "验证返回");
			Logger.comment("数据库查询spuid");
			SpuPropertyWapper SpuPropertyWapper=new SpuPropertyWapper();
			Map spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
			Integer spuId=(Integer) spuinfo.get("SpuId");
			Integer status=(Integer) spuinfo.get("Status");
			Logger.debug(spuinfo);
			Logger.verifyEquals(-2, status, "验证新同步数据status为-2");
			
			Logger.comment(spuId+":更新状态为-1删除");
			SpuPropertyWapper.updateSpuStatusDeleteById(spuId);
			
			passBean.setAuditor("autoTest");
			passBean.setSPUId(spuId);
			passCall.setData(passBean);
			passCall.callService();			
			Logger.verifyEquals("200", passCall.getString("Status"), "验证Status");
			
			spuinfo = SpuPropertyWapper.selectSpuProductInfoByProductId(ProductId);
			status=(Integer) spuinfo.get("Status");
			Logger.verifyEquals(-1, status, "验证status为-1不变");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}